<?php

class Controller_Admin_Brands extends Controller_Template
{

	protected $_limit = '';
	protected $_shop_info = '';
	public function __construct($req, $res)
	{
	
		parent::__construct($req, $res);
		$this->template	= 'smarty:admin/brands/index';
		$this->_limit	= 20;
		$this->_shop_info	= new Model_Shopinfo();
	}

	public function action_index()
	{
		$this->action_list();
	}

	public function action_list($merchant_id=0)
	{
		$page   = $this->request->query('page');
		$page	= $page ? intval($page) : 1;

		$offset	= $this->_limit * ($page - 1);

		$brand_name	= strval(trim($this->request->query('brand_name')));

		$merchant_id= intval($this->request->query('merchant_id'));

		$shop_id = 0;

		if($merchant_id > 0)
			$shop_id = User::instance(intval($merchant_id))->shop_id();

		//某个商家下面的所有品牌及分类	
		if(intval($merchant_id)>0 && intval($shop_id)>0)
		{
			$data   = Category_Brand::by_shop_page($shop_id,99,$offset,$this->_limit);
			$count  = Category_Brand::count_by_shop($shop_id,99); 
		}
		elseif($this->request->query('brand_name'))
		{
			$data	= Category_Brand::brand_by_name($brand_name, $this->_limit, $offset);
			$count  = Category_Brand::count_brand_by_name($brand_name);
		}
		else
		{
			$data	= Category_Brand::get_all_brand($this->_limit, $offset);
			$count  = Category_Brand::count_all_brand(0,0); 
		}
		
		for($i=0; $i< count($data); $i++)
		{
            $cate_ids = Category_brand::get_category_by_name($data[$i]['name']);
			$cname = '';
			$cate_name = array();
			if(!empty($cate_ids))
			{
				$cate_ids	= Category_brand::show_brand_category_ids($cate_ids);
				
				foreach($cate_ids as $cate_id)
				{
					$parent_ids	= Category_System::parent_ids($cate_id);

					$cate_name	= array();

					for($k=0; $k < count($parent_ids); $k++)
						$cate_name[] = & Category_system::name($parent_ids[$k]);
				
					Category_system::commit_names();

					foreach($cate_name as $cate)
					{
						$cname .= $cate.' | ';
					}
					$cname .= "<br/>";
					
					$data[$i]['cate_name']	.= $cname;
					
					$cname  = '';
				}
			}
					
			//$data[$i]['apply_uname']	= isset($data[$i]['apply_uid']) ? User::name($data[$i]['apply_uid']) : '';

			$data[$i]['create_time']	= date('Y-m-d H:i:s', $data[$i]['create_time']);

			$data[$i]['status_name']	= $data[$i]['status'] ? '已通过' : '未通过';

		}

		$page_config	= array(
							'total_items'	=> $count,
							'items_per_page'=> $this->_limit,
							);
		$pagination		= Pagination::factory($page_config);
		
		$cate_sys = new Category_System();
		$this->template->set('category_sys_1', $cate_sys->get_list());

		$this->template->set('total', $count);
		$this->template->set('pagination', $pagination->render());
		$this->template->set('datas', $data);
		$this->template->set('function_name', 'brands');	
		$this->template->set('brand_name', $brand_name);
		$this->template->set('shop_name', $shop_name);
		$this->template->set('page_title', '品牌列表');
		$this->response->body($this->template->render());
	}

	public function action_search()
	{
		$merchant_name	= strval($this->request->post('merchant_name'));

		$brand_name		= strval(trim($this->request->post('brand_name')));

		$category_id	= intval($this->request->post('category_id'));

		if(empty($merchant_name) || empty($brand_name))
		{
			$this->action_list(0);		
		
		}else{

			$rs	= Category_brand::brand_ids($brand_name);

			$cb	= new Category_brand();

			$cate_ids	= '';

			for($i=0; $i< count($rs); $i++)
			{
				$datas	= $cb->get_info($rs);//品牌数组
		
				$cate_ids[] = $datas;
			}
			

		}
		$this->template->set('datas', $datas);
		$this->template->set('function_name', 'brands');	
		$this->template->set('page_title', '品牌列表');
		$this->response->body($this->template->render());
	}

	public function action_add()
	{
		$cate_sys = new Category_System();
	
		$this->template->set('async_vars', Upimage::build_url('preview_img',  '11',  'brand_logo'));
		$this->template->set_filename('smarty:admin/brands/add');
		$this->template->set('category_sys_1', $cate_sys->get_list());
		$this->template->set('page_title', '新增品牌');
		$this->response->body($this->template->render());

	}

	public function action_insert()
	{
		$brand_name	= strval($this->request->post('brand_name'));
		$description= strval($this->request->post('description'));
		$img_id		= UpImage::get_upload_imgids(); 
		
		$cate_ids	= $this->category_info();
		
		$brand_info	= array('name' => $brand_name, 'description' => $description);

		$category_id= $cate_ids;
	
		if(! empty($category_id))
		{
			$img_id		= UpImage::get_upload_imgids(); 
			
			$cb			= new Category_Brand();

			$rs			= $cb->add_brand_and_category($brand_info, $img_id ,$category_id);

		}
		
		$this->request->redirect('/brands/list', $code= 302);		
	}

	protected function category_info()
	{
		$cate_1		= $this->request->post('category_sys_1');

		$cate_2		= $this->request->post('category_sys_2');

		$cate_3		= $this->request->post('category_sys_3');

		$cate_4		= $this->request->post('category_sys_4');

		$leaf_ids	= array();
	
		$parents	= array();
	
		if(is_array($cate_4) && !empty($cate_4))
		{
			for($i=0; $i < count($cate_4); $i++)
			{
				if(!empty($cate_4[$i]))
				{
					$leaf_ids[]	= $cate_4[$i];
				
					$parents[]	= Category_System::parent_id($cate_4[$i]);
				}
			}
		}

		if(is_array($cate_3) && !empty($cate_3))
		{
			for($i=0; $i < count($cate_3); $i++)
			{
				if(!empty($cate_3[$i]))
					$parents[]	= Category_System::parent_id($cate_3[$i]);

				if(in_array($cate_3[$i], $parents))
					continue;

				if(!empty($cate_3[$i]))
					$leaf_ids[]	= $cate_3[$i];
			}
		}
		if(is_array($cate_2) && !empty($cate_2))
		{
			for($i=0; $i < count($cate_2); $i++)
			{
				if(!empty($cate_2[$i]))
					$parents[]	= Category_System::parent_id($cate_2[$i]);

				if(in_array($cate_2[$i], $parents))
					continue;

				if(!empty($cate_2[$i]))
					$leaf_ids[]	= $cate_2[$i];
			}
		}
		if(is_array($cate_1) && !empty($cate_1))
		{
			for($i=0; $i < count($cate_1); $i++)
			{
				if(!empty($cate_1[$i]))
					$parents[]	= Category_System::parent_id($cate_1[$i]);

				if(in_array($cate_1[$i], $parents))
					continue;

				if(!empty($cate_1[$i]))
					$leaf_ids[]	= $cate_1[$i];

			}
		}

		return $leaf_ids;
	}
}

?>
